package com.example.demo.thread;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.AppVariable;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.controller.ArticleController;
import com.example.demo.entity.Articleinfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static java.time.LocalDateTime.now;
import static java.time.chrono.ChronoLocalDateTime.timeLineOrder;

public class MyThread implements Runnable{




    private  Integer uid;
    private  Integer id;
    private  String time;
    @Autowired
    private  ArticleService articleService;

    public MyThread(Integer uid,Integer id,String time,ArticleService articleService){
        this.uid = uid;
        this.id = id;
        this.time = time;
        this.articleService = articleService;
    }

    @Override
    public void run() {
        //创建一个新的线程，等时间到了再执行发布

        String now = now().toString();
        String nowTime = now.substring(0,10)+" "+now.substring(11,19);

        while(true){
            try {
                System.out.println("定时发布的时间"+time);
                System.out.println("当前时间"+nowTime);
                if (nowTime.compareTo(time)>=0){ //当前时间大于传来的时间time
//                    HttpSession session = request.getSession(false);
//                    Userinfo userinfo = (Userinfo) session.getAttribute(AppVariable.USER_SESSION_KEY);
//                    System.out.println("测试内容，当前登录的用户："+userinfo);

                    //先根据id，查询草稿内容
                    Articleinfo draft = articleService.getDraftById(id);
                    System.out.println("测试内容，time:"+time);
                    //再将查询到的草稿插入到文章表中
                    AjaxResult result = add1(uid,draft,time);
                    System.out.println("result"+result);
                    System.out.println("add1成功");

                    break;
                }
                System.out.println("等待子线程执行-------");
                now = now().toString();
                nowTime = now.substring(0,10)+" "+now.substring(11,19);
                Thread.sleep(1000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public AjaxResult add1(Integer uid, Articleinfo articleinfo, String time) {
        //1.非空校验
        if (articleinfo==null || !StringUtils.hasLength(articleinfo.getTitle())
                || !StringUtils.hasLength(articleinfo.getContent())){
            return AjaxResult.fail(-1,"非法参数");
        }
        //2.数据库添加操作
        //a.得到当前登录的uid  已经传过来了
//        Userinfo userinfo = (Userinfo) session.getAttribute(AppVariable.USER_SESSION_KEY);
//        if ((userinfo==null || userinfo.getId()<=0)){
//            //无效的登录用户
//            return AjaxResult.fail(-2,"无效的登录用户");
//        }
        articleinfo.setUid(uid);
        //设置当前时间
        String pattern = "yyyy-MM-dd HH:mm:ss";
        System.out.println("测试内容，time:"+time);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime createTime = LocalDateTime.parse(time,formatter);
        articleinfo.setCreatetime(createTime);
        articleinfo.setUpdatetime(createTime);
        System.out.println("完成了add1");

        return AjaxResult.success(articleService.add(articleinfo));
    }



}
